package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.News;
import com.example.demo.mapper.NewsMapper;
import com.example.demo.service.INewService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @descreption:
 * @author: xhr
 * @date: 2022/08/18
 */
@RestController
@RequestMapping("/news")
public class NewsController {
    @Resource
    NewsMapper newsMapper;
    @Resource
    INewService newService;

    //增
    @PostMapping
    public Result<?> save(@RequestBody News news) {
        news.setTime(new Date());
        newsMapper.insert(news);
        return Result.success();
    }

    //改(轮播图数据限制4条)
    @PutMapping
    public Result<?> update(@RequestBody News news) {
//        if(!news.getIsHot()){
        newsMapper.updateById(news);
        return Result.success();
//        }else{
//            List<News> newsList = newsMapper.selectList(new QueryWrapper<News>().eq("is_hot", true));
//            if (newsList.size() < 4) {
//                newsMapper.updateById(news);
//                return Result.success();
//            }else {
//                return Result.error("-1","轮播数据不能超过4条！");
//            }
//        }
    }

    //单独检查是否热门轮播
    @PutMapping("/changeHot")
    public Result<?> changeHot(@RequestBody News news) {
        if (!news.getIsHot()) {
            newsMapper.updateById(news);
            return Result.success();
        } else {
            List<News> newsList = newsMapper.selectList(new QueryWrapper<News>().eq("is_hot", true));
            if (newsList.size() < 4) {
                newsMapper.updateById(news);
                return Result.success();
            } else {
                return Result.error("-1", "轮播数据不能超过4条！");
            }
        }
    }

    // 删
    @DeleteMapping("/{id}")
    public Result<?> update(@PathVariable Long id) {
        newsMapper.deleteById(id);
        return Result.success();
    }

    //单查
    @GetMapping("/{newId}")
    public Result<?> findNew(@PathVariable Integer newId) {

        return Result.success(newsMapper.selectById(newId));
    }

    //全查
    @GetMapping
    public Result<?> findAll() {
        return Result.success(
                newsMapper.selectList(null)
        );
    }

    //查
    @GetMapping("/page")
    public Result<?> findPage(@RequestParam(defaultValue = "10") Integer pagesize,
                              @RequestParam(defaultValue = "1") Integer pagenum,
                              @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<News> wrapper = Wrappers.<News>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(News::getTitle, search);
        }
        return Result.success(newsMapper.selectPage(new Page<>(pagenum, pagesize), wrapper));
    }

    //给前台轮播图的
    @GetMapping("/front")
    public Result<?> findHot() {
        return Result.success(
                newsMapper.selectList(new QueryWrapper<News>().eq("is_hot", true))
        );
    }

    //给前台列表的
    @GetMapping("/frontList")
    public Result<?> findNoHot() {
        return Result.success(
                newsMapper.selectList(new QueryWrapper<News>().eq("is_hot", false))
        );
    }

}
